package com.service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class Services {

    public static final String DEFAULT_PROVIDER_NAME = "def";

    //构造器私有化
    private Services(){}

    public static final Map<String,Provider> providers = new ConcurrentHashMap<String,Provider>();

    public static void registerDefaultProvider(Provider provider){
        registerProvider(DEFAULT_PROVIDER_NAME, provider);
    }

    private static void registerProvider(String name, Provider provider){
        providers.put(name, provider);
    }

    public static Service newInstance(){
        return newInstance(DEFAULT_PROVIDER_NAME);
    }

    public static Service newInstance(String name){
        Provider p = providers.get(name);
        if(p == null){
            throw new IllegalArgumentException("no provider with name :" + name);
        }
        return p.newService();
    }
}